<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_live_data extends BZ_Model
{
/**
 * @section biskitZ Live Data Model
 */

	/**
	 * @function autocomplete()
	 * @description get the data for autocomplete
	 */
	public function autocomplete
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_query_or_like = array()
			, $prm_log_type = 'form' // avail values: form; modal
			, $prm_table_fields = 'id, name', $prm_fields_order = 'name asc'
			, $prm_table_where = ''
			, $prm_tot_row = 20
		)
	{
		if (trim($prm_table_name) != '')
		{
			/**
			 * @description load the model
			 */
			$this->load->model(array('M_autocomplete'));


			$autocomplete_data = $this->M_database->datas
				(
					$prm_dbase
					, $prm_table_name
					, $prm_table_where
					, $prm_table_fields
					, FALSE
					, $prm_fields_order
					, $prm_query_or_like
					, $prm_tot_row, 0
					, FALSE
					, FALSE
				);

			$return_value = '';
			if (is_array($autocomplete_data))
			{
				$return_value = $this->M_autocomplete->popup_list($autocomplete_data, array('id', 'name'));
			}


			/**
			 * @description log the action!
			 */
			$this->M_log->write
				(
					$prm_dbase
					, sprintf($this->lang->line('log_form_autocomplete'), $this->router->class, $prm_log_type, json_encode($return_value))
				);


			return $return_value;
		}
	}


	/**
	 * @function insert()
	 * @description insert the data
	 */
	public function insert
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_where = array(), $prm_data = array()
			, $prm_log_type = 'form'

			, $prm_update_auto = FALSE, $prm_js_url_redirect = ''
			, $prm_fields_auto_use = TRUE, $prm_fields_autoincrement_use = TRUE

			, $prm_object_start = 0, $prm_js_default_action_type = 'js_action_insert'
			, $prm_message_success = 'data_insert_success', $prm_message_error = 'data_insert_error'
		)
	{
		/**
		 * @description check is there any data for that key or not
		 */
		$data_count = $this->M_database->total_rows($prm_dbase, $prm_table_name, $prm_table_where);

		if ($data_count > 0)
		{
			/**
			 * @description there is a record, process the update data without confirmation dialog
			 */
			if ($prm_update_auto === TRUE)
			{
				$return_value = array
					(
						'js_action_success'
						, (trim($prm_js_url_redirect) === '')
							? array($this->M_database->update($prm_dbase, $prm_table_name, $prm_data, $prm_table_where, $prm_fields_auto_use, $prm_object_start), 'ok')
							: array($this->M_database->update($prm_dbase, $prm_table_name, $prm_data, $prm_table_where, $prm_fields_auto_use, $prm_object_start), 'ok', $prm_js_url_redirect)
					);
			}
			else
			{
				/**
				 * @description return as an update flag, so the controller can controll how to deal with the update and show the update confirmation dialog
				 */
				$return_value = array('js_action_update');
			}
		}
		else
		{
			/**
			 * @description there is NO record, process the REGULAR insert
			 */
			$return_value = array
				(
					$prm_js_default_action_type
					, (trim($prm_js_url_redirect) === '')
						? array($this->M_database->insert($prm_dbase, $prm_table_name, $prm_data, $prm_fields_auto_use, $prm_fields_autoincrement_use, $prm_object_start, $prm_message_success, $prm_message_error), 'ok')
						: array($this->M_database->insert($prm_dbase, $prm_table_name, $prm_data, $prm_fields_auto_use, $prm_fields_autoincrement_use, $prm_object_start, $prm_message_success, $prm_message_error), 'ok', $prm_js_url_redirect)
				);
		}


		/**
		 * @description log the action!
		 */
		if ($return_value[0] != 'js_action_update')
		{
			$this->M_log->write
				(
					$prm_dbase
					, sprintf
						(
							($prm_update_auto === TRUE && $data_count > 0) ? $this->lang->line('log_form_update') : $this->lang->line('log_form_insert')
							, $this->router->class, $prm_log_type, json_encode($prm_data)
						)
				);
		}


		return $return_value;
	}


	/**
	 * @function update()
	 * @description update the data
	 */
	public function update
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_where = array(), $prm_data = array()
			, $prm_log_type = 'form', $prm_js_action_type = 'js_action_update', $prm_js_url_redirect = ''
			, $prm_fields_auto_use = TRUE, $prm_object_start = 0
			, $prm_message_success = 'data_update_success', $prm_message_error = 'data_update_error'
		)
	{
		/**
		 * @description check is there any data for that key or not
		 */
		$data_count = $this->M_database->total_rows($prm_dbase, $prm_table_name, $prm_table_where);

		if ($data_count > 0)
		{
			/**
			 * @description REGULAR update the data
			 */
			$return_value = array
				(
					$prm_js_action_type
					, trim($prm_js_url_redirect) === ''
						? array($this->M_database->update($prm_dbase, $prm_table_name, $prm_data, $prm_table_where, $prm_fields_auto_use, $prm_object_start, FALSE, $prm_message_success, $prm_message_error), 'ok')
						: array($this->M_database->update($prm_dbase, $prm_table_name, $prm_data, $prm_table_where, $prm_fields_auto_use, $prm_object_start, FALSE, $prm_message_success, $prm_message_error), 'ok', $prm_js_url_redirect)
				);


			/**
			 * @description log the action!
			 */
			$this->M_log->write
				(
					$prm_dbase
					, sprintf($this->lang->line('log_form_update'), $this->router->class, $prm_log_type, json_encode($prm_data))
				);
		}
		else
		{
			$return_value = array
				(
					$prm_js_action_type
					, array($this->lang->line($prm_message_error), 'error')
				);
		}


		return $return_value;
	}


	/**
	 * @function delete()
	 * @description delete the data
	 */
	public function delete
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_key = '', $prm_key_posted = array(), $prm_table_where = array()
			, $prm_log_type = 'form', $prm_redirect_to_view = FALSE, $prm_return_type = 'json'
		)
	{
		if (trim($prm_key) != '')
		{
			/**
			 * @description this one if the user click the delete link from the table list
			 */
			$prm_log_type = 'view';
			$action_type = 'js_action_delete_list';

			if ( ! is_array($prm_key))
			{
				$table_where = explode('/', $prm_key);
			}

			$table_where = array_combine($prm_table_where, $table_where);
		}
		else
		{
			$action_type = 'js_action_delete';
			$prm_key_posted ? $table_where = array_combine($prm_table_where, $prm_key_posted) : redirect($this->tbl_attr['URL']['VIEW']);
		}


		$return_delete = $this->M_database->delete($prm_dbase, $prm_table_name, $table_where);

		switch ($return_delete)
		{
			case '1451':
				/**
				 * @description error constraint
				 */
				switch ($prm_return_type)
				{
					case 'boolean':
						$return_value = 'DELETE_ERROR_CONSTRAINT';
						break;


					default:
						$delete_status = sprintf($this->lang->line('data_error_constraint'), $this->lang->line('data_delete_msg'));

						$return_value = array('js_action_message_not_clear', array($delete_status));
						break;
				}
				break;


			default:
				switch ($prm_return_type)
				{
					case 'boolean':
						$return_value = 'DELETE_SUCCESS';
						break;


					default:
						$delete_status = $return_delete;

						if ($prm_redirect_to_view)
						{
							$return_value = array($action_type, array($return_delete, 'error', $this->tbl_attr['URL']['VIEW']));
						}
						else
						{
							$return_value = array($action_type, array($return_delete, 'error'));
						}
						break;
				}
				break;
		}


		/**
		 * @description log the action!
		 */
		$this->M_log->write
			(
				$prm_dbase
				, sprintf($this->lang->line('log_form_delete'), $this->router->class, $prm_log_type, json_encode($table_where), ($prm_return_type == 'boolean') ? $return_value : $delete_status)
			);


		return $return_value;
	}


	/**
	 * @function put()
	 * @description put the data to the form
	 */
	public function put
		(
			$prm_dbase = 'db_default'
			, $prm_table_name, $prm_table_where = array(), $prm_table_fields = '*'
			, $prm_object_start = '0', $prm_log_lang = 'log_form_edit', $prm_log_type = 'form'

			, $prm_js_action_type = 'js_action_data', $prm_function = array()
			, $prm_redirect_use = TRUE, $prm_redirect_url = ''
			, $prm_query_special = FALSE

			, $prm_css_replace = 'content_replace'
		)
	{
		$return_value = array
			(
				$prm_js_action_type
				, $this->M_database->datas
					(
						$prm_dbase
						, $prm_table_name
						, $prm_table_where
						, $prm_table_fields
						, TRUE
						, ''
						, ''
						, 1, 0
						, $prm_query_special
						, $prm_redirect_use, $prm_redirect_url
					)
				, $prm_object_start
			);

		switch ($prm_js_action_type)
		{
			case 'js_action_data_detail':
				/**
				 * @description generate detail viewlist
				 */
				$return_value = array_merge
					(
						$return_value
						, array(array($prm_css_replace, call_user_func_array($prm_function['FUNCTION_NAME'], $prm_function['FUNCTION_ATTR'])))
					);
				break;
		}


		/**
		 * @description log the action!
		 */
		$this->M_log->write
			(
				$prm_dbase
				, sprintf($this->lang->line($prm_log_lang), $this->router->class, $prm_log_type, json_encode($return_value))
			);


		return $return_value;
	}



}

/** End of file M_live_data.php */
/** Location: ./biskitz_mpc/biskitz/models/admin/m_live_data.php */
